import Sprite from "./Sprite";
import ballState from "../global/ballState";

export default class Ball extends Sprite {
  constructor() {
    super({
      x: 100,
      y: 100,
      radius: 10
    })
    this.flag = false;
    this.state = ballState.PADDING;
    this.vx = 2;
    this.vy = 5;
    this.type = "ball";
  }
  draw(gd) {
    gd.save();

    gd.beginPath();
    gd.arc(this.x, this.y, this.radius, 0, Math.PI * 2, true);
    gd.closePath();
    gd.fillStyle = "red";
    gd.fill();

    gd.restore();
  }
  move() {
    if (this.flag) {
      this.x += this.vx;
      this.y += this.vy;
    }
  }
  collision(cvs, body,mp3) {
    // 左右
    if (this.x - this.radius <= 0 || this.x + this.radius >= cvs.width) {
      this.vx = -this.vx;
    }
    // 上
    else if (this.y - this.radius <= 0) {
      this.vy = -this.vy;
    }
    // 下
    else if (this.y + this.radius >= cvs.height) {
      this.x = body.x + body.w / 2;
      this.y = body.y - this.radius;
      this.flag = false;
      this.state = ballState.LOSE;
    }
    // 物体之间碰撞
    else{
      let collision = body.collisionArc(this);
      if (body.type === "plate" && collision.collided && collision.position == 'y') {
        this.vy = -this.vy;
      }else{
      }
    }
    
  }
}